using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

/****************************************************
// 功能：血条及伤害显示
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/27 14:59:20
*****************************************************/

public class HpWindow : BaseWindow
{
    public Transform hpItemRoot;
    public Transform jumpNumRoot;
    public int jumpNumCount;
    
    private Dictionary<MainLogicUnit, ItemHp> itemDic = new Dictionary<MainLogicUnit, ItemHp>();
    public JumpPool pool;

    protected override void InitWindows()
    {
        base.InitWindows();
        itemDic = new Dictionary<MainLogicUnit, ItemHp>();
    }

    public void AddHpItemInfo(MainLogicUnit unit, Transform trans, int hp)
    {
        // 添加血条映射时就需要将伤害治疗窗口初始化，要不然获取队列会为null，这可能是项目架构设计上的缺陷
        pool = new JumpPool(jumpNumCount, jumpNumRoot);
        
        if (itemDic.ContainsKey(unit))
        {
            Debug.LogError("血条已经存在");
        }
        else
        {
            var path = GetItemPath(unit.unitType);
            var itemRoot = GameRoot.Instance.gameObject.transform.Find("WindowRoot/HpWindow(Clone)/ItemRoot").GetComponent<Transform>();
            var game = ResourceManager.Instance.LoadPrefab(path, itemRoot, true);
            game.transform.localPosition = Vector3.zero;
            game.transform.localScale = Vector3.one;

            var item = game.GetComponent<ItemHp>();
            item.InitItem(unit, trans, hp);
            itemDic.Add(unit, item);
        }
    }

    /// <summary>
    /// 获取血条类型
    /// </summary>
    /// <param name="unitType"></param>
    /// <returns></returns>
    private string GetItemPath(UnitTypeEnum unitType)
    {
        string path = "";
        switch (unitType)
        { 
            case UnitTypeEnum.Hero:
                path = "Prefab/DynamicItem/ItemHPHero";
                break;
            case UnitTypeEnum.Soldier:
                path = "Prefab/DynamicItem/ItemHPSoldier";
                break;
            case UnitTypeEnum.Tower:
                path = "Prefab/DynamicItem/ItemHPTower";
                break;
        }

        return path;
    }

    
    
    /// <summary>
    /// 更新血条
    /// </summary>
    /// <param name="key"></param>
    /// <param name="hp"></param>
    /// <param name="jump"></param>
    public void SetHpValue(MainLogicUnit key, int hp, JumpUpdateInfo jump)
    {
        if (itemDic.TryGetValue(key, out var item))
        {
            item.UpdateHpProgress(hp);
        }

        if (jump == null) return;
        var jn = pool.PopQueue();
        if (jn != null)
        {
            jn.Show(jump);
        }
    }

    public void SetJumpUpdateInfo(JumpUpdateInfo jump)
    {
        if (jump == null) return;
        var jn = pool.PopQueue();
        if (jn != null)
        {
            jn.Show(jump);
        }
    }

    public void RemoveHpItem(MainLogicUnit key)
    {
        if (!itemDic.TryGetValue(key, out var item)) return;
        Destroy(item.gameObject);
        itemDic.Remove(key);
    }

    protected override void ClearWindows()
    {
        base.ClearWindows();
        
        // 窗口关闭清理血条
        /*for (var i = 0; i < hpItemRoot.childCount; i++)
        {
            Destroy(hpItemRoot.GetChild(i));
        }*/
        itemDic?.Clear();

        /*for (var i = 0; i < jumpNumRoot.childCount; i++)
        {
            Destroy(jumpNumRoot.GetChild(i));
        }*/
    }

    public void SetStateInfo(MainLogicUnit key, StateEnum state, bool show = true)
    {
        if (itemDic.TryGetValue(key, out var item))
        {
            item.SetStateInfo(state, show);
        }
    }

    public class JumpPack {
        public JumpNumWindow jn;
        public JumpUpdateInfo jui;
    }
}
